package org.example.java8time;

import org.junit.Test;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @Classname LocalDateTimeTest
 * @Description localDateTime相关api
 * @Date 2021/2/4 2:54 下午
 * @Created by xiangjd
 */
public class LocalDateTimeTest {


    /**
     * localDateTime 相关api
     */
    @Test
    public void test1() {
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime.getDayOfYear());
        System.out.println(localDateTime.getDayOfMonth());
        System.out.println(localDateTime.getDayOfWeek());
        System.out.println(localDateTime.getMonth());
        System.out.println(localDateTime.getMonthValue());
        System.out.println(localDateTime.getHour());
        System.out.println(localDateTime.getMinute());
        System.out.println(localDateTime.getSecond());
        System.out.println(localDateTime.minusHours(1));
        System.out.println(localDateTime.minusMinutes(1));
        System.out.println(localDateTime.minusSeconds(10));
        System.out.println(localDateTime.plusHours(1));
        System.out.println(localDateTime.plusMinutes(1));
        System.out.println(localDateTime.plusSeconds(10));
    }

    /**
     * 2个localDateTime之间相差多少
     */
    @Test
    public void test3() {
        LocalDateTime localDateTime = LocalDateTime.now();
        LocalDateTime localDateTime1 = LocalDateTime.of(2019, 9, 16, 20, 59);
        Duration duration = Duration.between(localDateTime, localDateTime1);

        System.out.println(duration);
        long days = duration.toDays(); //相差的天数
        long hours = duration.toHours();//相差的小时数
        long minutes = duration.toMinutes();//相差的分钟数
        long millis = duration.toMillis();//相差毫秒数
        long nanos = duration.toNanos();//相差的纳秒数
        System.out.println(days + "-" + hours + "-" + minutes + "-" + millis + "-" + nanos);
    }

    @Test
    public void test2(){
        //LocalDateTime转String
        String format = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        System.out.println(format);
//        LocalDateTime localDateTime=LocalDateTime.now();
//        System.out.println(localDateTime);
//        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//        System.out.println(localDateTime.format(dtf2));

        //String转LocalDateTime
        LocalDateTime parse = LocalDateTime.parse("2020-01-17 11:52:04", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        System.out.println(parse);

        //LocalDateTime转Date
        Date from = Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant());
        System.out.println(from);

        //Date转LocalDateTime
        LocalDateTime localDateTime = LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault());
        System.out.println(localDateTime);
    }




}
